package com.haorui.lygyd.authority.utils;

import com.haorui.base.core.dto.AuthDto;
import com.haorui.base.core.utils.ApplicationContextUtil;
import com.haorui.base.system.entity.Account;
import com.haorui.base.system.service.AccountService;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author 陈刚
 * @version 1.0
 * @project ZNSM
 * @package com.haorui.base.core.utils
 * @createDate 2017-09-20 8:08
 * @modfiyDate
 * @function
 */
public class LoginUtil {

    public static Account getCurrentLoginAccount() {
        if (SecurityContextHolder.getContext().getAuthentication() != null) {
            Object obje = SecurityContextHolder.getContext().getAuthentication().getDetails();
            if (obje instanceof AuthDto) {
                AuthDto userDetails = (AuthDto) obje;
                AccountService accountService = (AccountService) ApplicationContextUtil.getContext().getBean("accountService");
                return accountService.findAccountByUserName(userDetails.getUserName());
            } else {
                return null;
            }

        } else {
            return null;
        }
    }

    public static String getLoginAccounName() {
        Account account = getCurrentLoginAccount();
        if (account == null) {
            return "系统";
        } else {
            return getCurrentLoginAccount().getRealName();
        }
    }
}
